package com.itheima.demo;

import java.io.*;
import java.util.Arrays;

public class Demo02 {//读取文件中的数据，排序后再写会本地文件

    public static void main(String[] args) throws IOException {
//        BufferedWriter bw = new BufferedWriter(new FileWriter("day016\\sort.txt"));
//        bw.write("5,9,7,2,1,3,6,10,4,8");
//        bw.close();
        //读取数据
        BufferedReader br = new BufferedReader(new FileReader("day016\\sort.txt"));
        String s = br.readLine();
        br.close();//释放资源
        System.out.println("我读到的数据是:" + s);
        //切割字符
        String[] splits = s.split(" ");
        //将字符转为数字
        int[] array = new int[splits.length];//存储数据
        for (int i = 0; i < splits.length; i++) {
            array[i] = Integer.parseInt(splits[i]);//将字符串数组中的字符串转为Int,再赋值给对应Int数组位置
        }
        //快排
        Arrays.sort(array);
        //重写数据
        BufferedWriter bw = new BufferedWriter(new FileWriter("day016\\sort.txt"));
        for (int i = 0; i < array.length; i++) {
            bw.write(array[i]+" ");
        }
        //释放资源
        bw.close();
    }
}
